package com.ep.common.plugins;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;

public class ControllerGenerator {
    public void generate(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        generateController(interfaze, topLevelClass, introspectedTable);
    }

    private void generateController(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        // 获取表名和类名
        String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName();
        String entityNameShort = tableName.substring(tableName.indexOf("_") + 1);
        String entityName =  CodeGenerationUtils.toCamelCase(tableName);
        String controllerName = entityName + "Controller";
        String serviceName = entityName + "Service";
        String serviceNameFirstCharToLower = CodeGenerationUtils.firstCharToLower(serviceName);
        //String serviceMenuName = entityName + "MenuService";
        //String entityClassName = entityName + "Entity";
        String entityClassName = entityName;

        // 创建控制器类
        TopLevelClass controllerClass = new TopLevelClass("com.ep.modules.app.controller." + controllerName);
        controllerClass.setVisibility(JavaVisibility.PUBLIC);
        controllerClass.addAnnotation("@RestController");
        controllerClass.addAnnotation("@RequestMapping(\"/app/" + entityNameShort.toLowerCase() + "\")");
        controllerClass.addImportedType("org.springframework.web.bind.annotation.*");
        controllerClass.addImportedType("org.springframework.beans.factory.annotation.*");
        //controllerClass.addImportedType("org.apache.shiro.authz.annotation.RequiresPermissions");
        controllerClass.addImportedType("com.ep.common.annotation.SysLog");
        controllerClass.addImportedType("com.ep.common.utils.R");
        controllerClass.addImportedType("com.ep.common.utils.PageUtils");
        controllerClass.addImportedType("com.ep.common.validator.ValidatorUtils");
        //controllerClass.addImportedType("com.ep.common.utils.Constant");
        controllerClass.addImportedType("com.ep.modules.app.entity." + entityClassName);
        controllerClass.addImportedType("com.ep.modules.app.service." + serviceName);
        //controllerClass.addImportedType("java.util.HashMap");
       // controllerClass.addImportedType("java.util.List");
        controllerClass.addImportedType("java.util.Map");
        //controllerClass.addImportedType("com.ep.modules.app.service." + serviceMenuName);

        // 添加服务注入
        Field serviceField = new Field(serviceNameFirstCharToLower, new FullyQualifiedJavaType("com.ep.modules.app.service." + serviceName));
        serviceField.setVisibility(JavaVisibility.PRIVATE);
        serviceField.addAnnotation("@Autowired");
        controllerClass.addField(serviceField);

//        Field serviceMenuField = new Field(serviceMenuName, new FullyQualifiedJavaType("com.ep.modules.app.service." + serviceMenuName));
//        serviceMenuField.setVisibility(JavaVisibility.PRIVATE);
//        serviceMenuField.addAnnotation("@Autowired");
//        controllerClass.addField(serviceMenuField);

        // 添加列表方法
        Method listMethod = new Method("list");
        listMethod.setVisibility(JavaVisibility.PUBLIC);
        listMethod.setReturnType(new FullyQualifiedJavaType("R"));
        listMethod.addParameter(new Parameter(new FullyQualifiedJavaType("Map<String, Object>"), "params"));
        listMethod.addAnnotation("@GetMapping(\"/list\")");
        //listMethod.addAnnotation("@RequiresPermissions(\"app:" + entityNameShort.toLowerCase() + ":list\")");
//        listMethod.addBodyLine("if(getUserId() != Constant.SUPER_ADMIN){");
//        listMethod.addBodyLine("params.put(\"createUserId\", getUserId());");
//        listMethod.addBodyLine("}");
        listMethod.addBodyLine("PageUtils page = " + serviceNameFirstCharToLower+ ".queryPage(params);");
        listMethod.addBodyLine("return R.ok().put(\"page\", page);");
        controllerClass.addMethod(listMethod);

//        // 添加选择方法
//        Method selectMethod = new Method("select");
//        selectMethod.setVisibility(JavaVisibility.PUBLIC);
//        selectMethod.setReturnType(new FullyQualifiedJavaType("R"));
//        selectMethod.addAnnotation("@GetMapping(\"/select\")");
//        selectMethod.addAnnotation("@RequiresPermissions(\"app:" + entityNameShort.toLowerCase() + ":select\")");
//        selectMethod.addBodyLine("Map<String, Object> map = new HashMap<>();");
////        selectMethod.addBodyLine("if(getUserId() != Constant.SUPER_ADMIN){");
////        selectMethod.addBodyLine("map.put(\"create_user_id\", getUserId());");
////        selectMethod.addBodyLine("}");
//        selectMethod.addBodyLine("List<" + entityClassName + "> list = (" + "List<" + entityClassName + ">) " + serviceNameFirstCharToLower+ ".listByMap(map);");
//        selectMethod.addBodyLine("return R.ok().put(\"list\", list);");
//        controllerClass.addMethod(selectMethod);

        // 添加信息方法
        Method infoMethod = new Method("info");
        infoMethod.setVisibility(JavaVisibility.PUBLIC);
        infoMethod.setReturnType(new FullyQualifiedJavaType("R"));

        Parameter infoParameter = new Parameter(new FullyQualifiedJavaType("Long"), "id");
        infoParameter.addAnnotation("@PathVariable");

        infoMethod.addParameter(infoParameter);
        infoMethod.addAnnotation("@GetMapping(\"/info/{id}\")");
        //infoMethod.addAnnotation("@RequiresPermissions(\"app:" + entityNameShort.toLowerCase() + ":info\")");
        infoMethod.addBodyLine(entityClassName + " " + entityName.toLowerCase() + " = " + serviceNameFirstCharToLower + ".getById(id);");
       // infoMethod.addBodyLine("List<Long> menuIdList = " + serviceMenuName + ".queryMenuIdList(id);");
       // infoMethod.addBodyLine(entityName.toLowerCase() + ".setMenuIdList(menuIdList);");
        infoMethod.addBodyLine("return R.ok().put(\"" + entityName.toLowerCase() + "\", " + entityName.toLowerCase() + ");");
        controllerClass.addMethod(infoMethod);

        // 添加保存方法
        Method saveMethod = new Method("save");
        saveMethod.setVisibility(JavaVisibility.PUBLIC);
        saveMethod.setReturnType(new FullyQualifiedJavaType("R"));
        saveMethod.addParameter(new Parameter(new FullyQualifiedJavaType("@" + "RequestBody " + entityClassName), entityName.toLowerCase()));
        saveMethod.addAnnotation("@SysLog(\"保存" + entityName + "\")");
        saveMethod.addAnnotation("@PostMapping(\"/save\")");
        ///saveMethod.addAnnotation("@RequiresPermissions(\"app:" + entityNameShort.toLowerCase() + ":save\")");
        saveMethod.addBodyLine("ValidatorUtils.validateEntity(" + entityName.toLowerCase() + ");");
        //saveMethod.addBodyLine(entityName.toLowerCase() + ".setCreateUserId(getUserId());");
        saveMethod.addBodyLine(serviceNameFirstCharToLower + ".save(" + entityName.toLowerCase() + ");");
        saveMethod.addBodyLine("return R.ok();");
        controllerClass.addMethod(saveMethod);

        // 添加更新方法
        Method updateMethod = new Method("update");
        updateMethod.setVisibility(JavaVisibility.PUBLIC);
        updateMethod.setReturnType(new FullyQualifiedJavaType("R"));
        updateMethod.addParameter(new Parameter(new FullyQualifiedJavaType("@" + "RequestBody " + entityClassName), entityName.toLowerCase()));
        updateMethod.addAnnotation("@SysLog(\"修改" + entityName + "\")");
        updateMethod.addAnnotation("@PostMapping(\"/update\")");
        //updateMethod.addAnnotation("@RequiresPermissions(\"app:" + entityNameShort.toLowerCase() + ":update\")");
        updateMethod.addBodyLine("ValidatorUtils.validateEntity(" + entityName.toLowerCase() + ");");
        //updateMethod.addBodyLine(entityName.toLowerCase() + ".setCreateUserId(getUserId());");
        updateMethod.addBodyLine(serviceNameFirstCharToLower + ".update(" + entityName.toLowerCase() + ");");
        updateMethod.addBodyLine("return R.ok();");
        controllerClass.addMethod(updateMethod);

        // 添加删除方法
        Method deleteMethod = new Method("delete");
        deleteMethod.setVisibility(JavaVisibility.PUBLIC);
        deleteMethod.setReturnType(new FullyQualifiedJavaType("R"));
        deleteMethod.addParameter(new Parameter(new FullyQualifiedJavaType("@" + "RequestBody Long[]"), "ids"));
        deleteMethod.addAnnotation("@SysLog(\"删除" + entityName + "\")");
        deleteMethod.addAnnotation("@PostMapping(\"/delete\")");
        //deleteMethod.addAnnotation("@RequiresPermissions(\"app:" + entityNameShort.toLowerCase() + ":delete\")");
        deleteMethod.addBodyLine(serviceNameFirstCharToLower + ".deleteBatch(ids);");
        deleteMethod.addBodyLine("return R.ok();");
        controllerClass.addMethod(deleteMethod);

        // 写入文件
        CodeGenerationUtils.writeToFile(controllerClass, "src/main/java/com/ep/modules/app/controller", controllerName+ ".java");

//        // 将控制器类添加到顶层类中com.ep.modules.app.controller.
//        // 确保 topLevelClass 不为 null
//        if (topLevelClass == null) {
//            topLevelClass = new TopLevelClass(interfaze.getType().getFullyQualifiedName());
//            topLevelClass.setVisibility(JavaVisibility.PUBLIC);
//            topLevelClass.addImportedType(interfaze.getType());
//        }
//        topLevelClass.addImportedType(controllerClass.getType());
//        topLevelClass.addInnerClass(controllerClass);
    }
}